/**
 * Notes: 表白墙业务逻辑 (新版API)
 * Ver : WeCollege Server API 1.0.0
 * Date: 2025-01-27
 * Description: 使用自建服务器API重构表白墙业务逻辑
 */

const BaseBiz = require('../../../comm/biz/base_biz.js');
const serverHelper = require('../../../helper/cloud_helper.js'); // 使用替换后的helper

class BoardBiz extends BaseBiz {

  // 表白墙类型ID
  static BOARD_TYPE_ID = 'board';

  /**
   * 发布表白墙
   * @param {object} data - 表白墙数据
   * @returns {Promise<object>} 发布结果
   */
  static async insert(data) {
    // 数据格式转换：云函数格式 -> 服务器API格式
    const postData = {
      title: data.formTitle,
      content: data.formContent,
      typeId: BoardBiz.BOARD_TYPE_ID,
      to: data.formTo || '',
      from: data.formFrom || '',
      pic: data.formPic || [],
      forms: data.forms || []
    };

    return await serverHelper.callCloudSumbit('board/insert', postData);
  }

  /**
   * 编辑表白墙
   * @param {object} data - 表白墙数据
   * @returns {Promise<object>} 编辑结果
   */
  static async edit(data) {
    const postData = {
      postId: data.id,
      title: data.formTitle,
      content: data.formContent,
      typeId: BoardBiz.BOARD_TYPE_ID,
      to: data.formTo || '',
      from: data.formFrom || '',
      pic: data.formPic || [],
      forms: data.forms || []
    };

    return await serverHelper.callCloudSumbit('board/edit', postData);
  }

  /**
   * 获取表白墙详情
   * @param {string} id - 表白墙ID
   * @returns {Promise<object>} 表白墙详情
   */
  static async getDetail(id) {
    const result = await serverHelper.callCloudData('board/detail', { 
      postId: id 
    });

    // 数据格式转换：服务器API格式 -> 云函数格式
    if (result && result.data) {
      const data = result.data;
      result.data = {
        BOARD_ID: data.postId,
        BOARD_TITLE: data.title,
        BOARD_CONTENT: data.content,
        BOARD_TO: data.to || '',
        BOARD_FROM: data.from || '',
        BOARD_PIC: data.pic || [],
        BOARD_ADD_TIME: data.createTimestamp,
        BOARD_EDIT_TIME: data.updateTimestamp,
        BOARD_VIEW_COUNT: data.visitNum || 0,
        BOARD_LIKE_COUNT: data.likeNum || 0,
        BOARD_COMMENT_COUNT: data.commentNum || 0,
        BOARD_STATUS: data.status,
        BOARD_ORDER: data.order || 9999,
        BOARD_VOUCH: data.vouch || 0,
        BOARD_USER: {
          USER_ID: data.user?.userId,
          USER_NAME: data.user?.nickName,
          USER_PIC: data.user?.image
        },
        BOARD_OBJ: data.forms || []
      };
    }

    return result;
  }

  /**
   * 获取表白墙列表
   * @param {object} params - 查询参数
   * @returns {Promise<object>} 表白墙列表
   */
  static async getList(params = {}) {
    const query = {
      typeId: BoardBiz.BOARD_TYPE_ID,
      pageNo: params.page || 1,
      pageSize: params.size || 20,
      search: params.search || '',
      sortType: params.sortType || 'time',
      sortVal: params.sortVal || 'desc',
      whereEx: params.whereEx || {},
      isTotal: true
    };

    const result = await serverHelper.callCloudData('board/list', query);

    // 数据格式转换：服务器API格式 -> 云函数格式
    if (result && result.data && result.data.list) {
      result.data.list = result.data.list.map(item => ({
        BOARD_ID: item.postId,
        BOARD_TITLE: item.title,
        BOARD_CONTENT: item.content,
        BOARD_TO: item.to || '',
        BOARD_FROM: item.from || '',
        BOARD_PIC: item.pic || [],
        BOARD_ADD_TIME: item.createTimestamp,
        BOARD_EDIT_TIME: item.updateTimestamp,
        BOARD_VIEW_COUNT: item.visitNum || 0,
        BOARD_LIKE_COUNT: item.likeNum || 0,
        BOARD_COMMENT_COUNT: item.commentNum || 0,
        BOARD_STATUS: item.status,
        BOARD_ORDER: item.order || 9999,
        BOARD_VOUCH: item.vouch || 0,
        BOARD_USER: {
          USER_ID: item.user?.userId,
          USER_NAME: item.user?.nickName,
          USER_PIC: item.user?.image
        },
        BOARD_OBJ: item.forms || []
      }));
    }

    return result;
  }

  /**
   * 访问表白墙 (增加浏览量)
   * @param {string} id - 表白墙ID
   * @returns {Promise<object>} 访问结果
   */
  static async view(id) {
    return await serverHelper.callCloudSumbit('board/view', { 
      postId: id 
    }, { title: 'bar' });
  }

  /**
   * 点赞表白墙
   * @param {string} id - 表白墙ID
   * @returns {Promise<object>} 点赞结果
   */
  static async like(id) {
    return await serverHelper.callCloudSumbit('board/like', { 
      postId: id 
    });
  }

  /**
   * 删除表白墙
   * @param {string} id - 表白墙ID
   * @returns {Promise<object>} 删除结果
   */
  static async del(id) {
    return await serverHelper.callCloudSumbit('board/del', { 
      postId: id 
    });
  }

  /**
   * 设置表白墙状态
   * @param {string} id - 表白墙ID
   * @param {number} status - 状态
   * @returns {Promise<object>} 设置结果
   */
  static async status(id, status) {
    return await serverHelper.callCloudSumbit('board/status', { 
      postId: id, 
      status: status 
    });
  }

  /**
   * 获取我的表白墙列表
   * @param {object} params - 查询参数
   * @returns {Promise<object>} 我的表白墙列表
   */
  static async getMyList(params = {}) {
    const query = {
      typeId: BoardBiz.BOARD_TYPE_ID,
      pageNo: params.page || 1,
      pageSize: params.size || 20,
      isMy: true
    };

    return await BoardBiz.getList(query);
  }

  /**
   * 搜索表白墙
   * @param {string} keyword - 搜索关键词
   * @param {object} params - 其他参数
   * @returns {Promise<object>} 搜索结果
   */
  static async search(keyword, params = {}) {
    const query = {
      ...params,
      search: keyword
    };

    return await BoardBiz.getList(query);
  }

  /**
   * 获取热门表白墙
   * @param {object} params - 查询参数
   * @returns {Promise<object>} 热门表白墙列表
   */
  static async getHotList(params = {}) {
    const query = {
      ...params,
      sortType: 'like',
      sortVal: 'desc'
    };

    return await BoardBiz.getList(query);
  }

  /**
   * 获取推荐表白墙
   * @param {object} params - 查询参数
   * @returns {Promise<object>} 推荐表白墙列表
   */
  static async getVouchList(params = {}) {
    const query = {
      ...params,
      whereEx: { vouch: 1 }
    };

    return await BoardBiz.getList(query);
  }
}

/** 表单校验 */
BoardBiz.CHECK_FORM = {
  title: 'formTitle|must|string|min:1|max:100|name=标题',
  content: 'formContent|must|string|min:1|max:50000|name=内容',
  to: 'formTo|false|string|max:30|name=表白对象',
  from: 'formFrom|false|string|max:30|name=表白者'
};

module.exports = BoardBiz;